package com.lin.consume;

import com.lin.common.constant.CustomerServiceConstant;
import com.lin.common.error.CustomRuntimeException;
import com.lin.socketio.dto.Encounter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;

import java.io.UnsupportedEncodingException;

public class EncounterRabbitmqConsumer {
    private static Logger log = LoggerFactory.getLogger(EncounterRabbitmqConsumer.class);

    @Value("${spring.rabbitmq.exchange.encounter.topic}")
    private String encounterTopic;

    /**
     * #{T(com.lin.common.utils.DateUtils).fullDateFormat()} 动态创建队列，是为了每个微服务都能获取到消息，例如TOPIC
     *
     * @param r 消息内容
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue("#{T(com.lin.common.utils.DateUtils).fullDateFormat()}"),  //系统自行 创建临时队列
                    exchange = @Exchange(value = "${spring.rabbitmq.exchange.encounter.topic}", type = CustomerServiceConstant.EXCHANGE_FANOUT)  //绑定的交换机
            )
    })
    public void encounterReceivel(Encounter e) throws UnsupportedEncodingException, CustomRuntimeException {
        log.info("EncounterRabbitmqConsumer on message body :{}", e.toString());
        ConsumerHandler cc = ConsumerHandler.getBean(encounterTopic + CustomerServiceConstant.CONSUMER_HANDLER);
        cc.onMessage(e);
         log.info("rabbitmq send client success");
    }
}
